package cn.com.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.com.biz.DepartmentsBiz;
import cn.com.biz.NewsBiz;
import cn.com.entity.News;

import com.opensymphony.xwork2.ActionSupport;

public class NewsAction extends ActionSupport implements SessionAware{
	private Map<String, Object> session;
	private String newsmsg;
	private News news;
	private NewsBiz newsbiz;
	public String getNewsmsg() {
		return newsmsg;
	}
	public void setNewsmsg(String newsmsg) {
		this.newsmsg = newsmsg;
	}
	public News getNews() {
		return news;
	}
	public void setNews(News news) {
		this.news = news;
	}
	public NewsBiz getNewsbiz() {
		return newsbiz;
	}
	public void setNewsbiz(NewsBiz newsbiz) {
		this.newsbiz = newsbiz;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		session=arg0;
	}

	//查询所有新闻
	public String selectAllNews(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		newsbiz=(NewsBiz)ac.getBean("newsbiz");
		List<News> list=newsbiz.selectAllNews();
		session.put("allnewslist", list);
		return SUCCESS;
	}
	
	//查询一条新闻
	public String selectOneNews(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		newsbiz=(NewsBiz)ac.getBean("newsbiz");
		News n=newsbiz.selectOneNews(news);
		session.put("onenewslist", n);
		return SUCCESS;
	}
	//添加一条新闻
	public String addNews(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		newsbiz=(NewsBiz)ac.getBean("newsbiz");
		boolean  bool=newsbiz.addNews(news);
		if(bool){
			setNewsmsg("添加新闻成功");
			return SUCCESS;
		}else{
			setNewsmsg("添加新闻失败，请重新添加");
			return ERROR; 
		}
	}
	//更新新闻
	public String updateNews(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		newsbiz=(NewsBiz)ac.getBean("newsbiz");
		boolean  bool=newsbiz.updateNews(news);
		if(bool){
			setNewsmsg("更新新闻成功");
			return SUCCESS;
		}else{
			setNewsmsg("更新新闻失败，请重新更新");
			return ERROR; 
		}
	}
	//删除新闻
	public String deleteNews(){
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		newsbiz=(NewsBiz)ac.getBean("newsbiz");
		boolean  bool=newsbiz.deleteNews(news);
		if(bool){
			setNewsmsg("删除新闻成功");
			return SUCCESS;
		}else{
			setNewsmsg("删除新闻失败，请重新删除");
			return ERROR; 
		}
	}
	
}
